package chenji.yourticket.editor;

import java.util.HashMap;
import java.util.HashSet;

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;

import chenji.yourticket.Main;
import chenji.yourticket.inventory.InventoryManager.OpeningInventoryType;
import chenji.yourticket.model.ModelInfo;

public class EditorManager {
	public HashMap<Player, MaterialChooseInventory> mci = new HashMap<Player, MaterialChooseInventory>();
	public HashMap<Player, EditorInventory> eit = new HashMap<Player, EditorInventory>();
	public HashSet<Player> ctl = new HashSet<Player>();
	public HashSet<Player> editorchat = new HashSet<Player>();
	public HashMap<Player, Material> pml = new HashMap<Player, Material>();

	public void newModelInfo(Player p) {
		int nowAmount = Main.getPlayerDataChenter().getPlayerData(p).getAmount();
		int maxAmount = Main.getLimitManager().getLimit(p);
		if (nowAmount >= maxAmount) {
			p.sendMessage(Main.getMessageManager().getMessage_CannotCreate());
		}
		MaterialChooseInventory c = new MaterialChooseInventory(p);
		c.openInventory(OpeningInventoryType.EditorManager);
		this.mci.put(p, c);
	}

	public void createNewModelInfo(Player p, Material material, String Name) {
		EditorInventory e = new EditorInventory(p, material, Name);
		e.openInventory(OpeningInventoryType.EditorManager);
		this.eit.put(p, e);
		this.mci.remove(p);

	}

	public boolean chatListener(Player p, String Message) {
		if (this.ctl.contains(p)) {
			if (Main.getPlayerDataChenter().getPlayerData(p).contains(Message)) {
				p.sendMessage(Main.getMessageManager().getContains());
				this.clearEditInfo(p);
				ctl.remove(p);
				return true;
			}
			if (this.pml.containsKey(p)) {
				this.createNewModelInfo(p, this.pml.get(p), Message);
				ctl.remove(p);
				return true;
			} else {
				p.sendMessage(Main.getMessageManager().getUnknown_Exception_ConnotFindMaterial());
				ctl.remove(p);
				this.clearEditInfo(p);
				return true;
			}
		}
		if (this.editorchat.contains(p)) {
			this.eit.get(p).setChat(Message);
			this.editorchat.remove(p);
			ctl.remove(p);
			return true;
		}
		return false;
	}

	public void clearEditInfo(Player p) {
		this.ctl.remove(p);
		this.mci.remove(p);
		this.eit.remove(p);
		this.pml.remove(p);
		p.closeInventory();

	}

	public void setNewModelMaterial(Player p, Material m) {
		this.pml.put(p, m);
		ctl.add(p);
	}

	public void registerChatListener(Player p) {
		this.editorchat.add(p);

	}

	public void clickInventory(Player p, int slot, Inventory inventory) {
		MaterialChooseInventory i = this.mci.get(p);
		if (i != null) {
			if (i.getInventory().equals(inventory)) {
				i.ClickInventory(slot);
				return;
			}
		}

		EditorInventory d = this.eit.get(p);
		if (d != null) {
			if (d.getInventory().equals(inventory)) {
				d.InventoryClick(slot);
				return;
			}
		}

	}

	public EditorInventory getEditorInventory(Player p) {
		return this.eit.get(p);
	}

	public void save(Player p) {
		EditorInventory i = this.getEditorInventory(p);
		if (!(i == null)) {
			i.saveModelInfo();
		}
	}

	public void edit(Player p, ModelInfo mi) {
		EditorInventory i = new EditorInventory(p, mi);
		i.openInventory(OpeningInventoryType.EditorManager);
		this.eit.put(p, i);
	}

	public void closeInventory(Player p) {
		if(this.eit.containsKey(p)){
			if(this.eit.get(p).canClear()){
				this.eit.get(p).saveModelInfo();
			}
		}
		
	}
	public void unload(){
		for(Player p:this.eit.keySet()){
			this.eit.get(p).saveModelInfo();
			p.closeInventory();
		}
		for(Player p: this.mci.keySet()){
			p.closeInventory();
		}
	}



}
